<template>
	<view class="login-container" :data-theme="theme()" :class="theme() || ''">
		<view class="skip" @click="gotoPage('/pages/index/index')">跳过→</view>
		<view class="" v-if="is_login != 0">
			<view class="login_topbpx">
				<view class="login_tit" v-if="is_login == 2">快速登录</view>
				<view class="login_tit" v-if="is_login == 1">账号登录</view>
				<view class="login_top" v-if="is_login == 2">首次登录会自动注册</view>
			</view>
			<view class="group-bd">
				<view class="form-level d-s-c">
					<input class="login-input" :adjust-position="false" type="text" v-model="formData.mobile"
						placeholder="请填写手机号" :disabled="is_send" />
				</view>
				<view class="form-level d-s-c" v-if="is_login == 1">
					<input class="login-input" :adjust-position="false" type="text" password="true"
						v-model="formData.password" placeholder="请输入密码" />
					<button class="get-code-btn" @click="is_login = 0">忘记密码？</button>
				</view>
				<view class="form-level d-b-c" v-if="is_login == 2 && sms_open">
					<input class="login-input" type="number" v-model="formData.code" placeholder="请填写验证码" />
					<button class="get-code-btn" @click="sendCode" :disabled="is_send">
						{{ send_btn_txt }}
					</button>
				</view>
			</view>
		</view>
		<view class="" v-if="is_login == 0">
			<view class="login_topbpx">
				<view class="login_tit">找回密码</view>
				<view class="login_top">请确认您的账号已绑定此手机号码</view>
			</view>
			<view class="group-bd">
				<view class="form-level d-s-c">
					<view class="val flex-1"><input type="text" v-model="resetpassword.mobile" placeholder="请填写手机号"
							:disabled="is_send" /></view>
				</view>
				<view class="form-level d-s-c">
					<view class="val flex-1 d-b-c">
						<input class="flex-1" :adjust-position="false" type="number" v-model="resetpassword.code"
							placeholder="请填写验证码" />
						<button class="get-code-btn" @click="sendCode" :disabled="is_send">
							{{ send_btn_txt }}
						</button>
					</view>
				</view>
				<view class="form-level d-s-c">
					<view class="val flex-1"><input type="text" :adjust-position="false" password="true"
							v-model="resetpassword.password" placeholder="请输入密码" /></view>
				</view>
				<view class="form-level d-s-c">
					<view class="val flex-1"><input type="text" :adjust-position="false" password="true"
							v-model="resetpassword.repassword" placeholder="请再次输入密码" /></view>
				</view>
			</view>
		</view>
		<view @click="isRead = !isRead" class="d-s-c gray6 mt30">
			<view class="icon iconfont icon-tijiaochenggong" :class="isRead ? 'active agreement' : 'agreement'"></view>
			我已阅读并接受<text class="dominant" @click="xieyi('service')">《用户协议》</text>和<text class="dominant"
				@click="xieyi('privacy')">《隐私政策》</text>
		</view>
		<button v-if="is_login == 2" class="theme-btn sub-btn" @click="formSubmit">
			立即登录
		</button>
		<button v-if="is_login == 1" class="theme-btn sub-btn" @click="formSubmit">
			立即登录
		</button>
		<button v-if="is_login == 0" class="theme-btn sub-btn" @click="resetpasswordSub">
			确认
		</button>
		<view class="f30 gray6 d-c-c">
			<view v-if="is_login == 1" @click="is_login = 2">手机登录</view>
			<view v-if="is_login == 2" @click="is_login = 1">账号登录</view>
			<view v-if="is_login == 0" @click="is_login = 1">立即登录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/*表单数据对象*/
				formData: {
					/*手机号*/
					mobile: "",
					password: "",
					/*验证码*/
					code: "",
				},
				resetpassword: {
					mobile: "",
					password: "",
					repassword: "",
					code: "",
				},
				/*是否已发验证码*/
				is_send: false,
				/*发送按钮文本*/
				send_btn_txt: "获取验证码",
				/*当前秒数*/
				second: 60,
				ip: "",
				is_login: 2,
				sms_open: false,
				isRead: false,
			};
		},
		onLoad(e) {
			// this.getData();
			// let self = this;
			// if (e.invitationId) {
			// 	uni.setStorageSync('invitationId', e.invitationId);
			// }
			// if (e.refereeId) {
			// 	uni.setStorageSync('refereeId', e.refereeId);
			// }
		},
		onShow() {
			this.getCodeType();
			// this.init()
		},

		methods: {
			xieyi(type) {
				this.gotoPage("/pages/webview/ue?type=" + type);
			},
			getCodeType() {
				let self = this;
				self._get("index/loginSetting", {}, (res) => {
					self.sms_open = res.data.h5SmsOpen;
					if (!self.sms_open) {
						self.is_login = 1;
					}
					// self.is_code = self.sms_open;
				});
			},
			/*初始化*/
			// init() {
			// 	let self = this;
			// 	uni.getSystemInfo({
			// 		success(res) {
			// 			self.phoneHeight = res.windowHeight;
			// 		}
			// 	});
			// },
			// getData(){
			// 	let self = this;
			// 	self._get('index/policy', {}, function(res) {
			// 		self.service = res.data.service;
			// 		self.privacy = res.data.privacy;
			// 	});
			// },
			/*提交*/
			formSubmit() {
				let self = this;
				if (!self.isRead) {
					uni.showToast({
						title: "请先阅读并接受用户协议及隐私政策",
						duration: 2000,
						icon: "none",
					});
					return;
				}
				let formdata = {
					mobile: self.formData.mobile,
					invitationId: self.invitationId || 0,
					refereeId: uni.getStorageSync("refereeId") || 0,
				};
				let url = "";
				if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.formData.mobile)) {
					console.log(self.formData.mobile);
					uni.showToast({
						title: "手机有误,请重填！",
						duration: 2000,
						icon: "none",
					});
					return;
				}
				if (self.is_login == 2) {
					if (self.sms_open && self.formData.code == "") {
						uni.showToast({
							title: "验证码不能为空！",
							duration: 2000,
							icon: "none",
						});
						return;
					}
					formdata.code = self.formData.code;
					url = "user/userOpen/smsLogin";
				} else {
					formdata.password = self.formData.password;
					if (self.formData.password == "") {
						uni.showToast({
							title: "密码不能为空！",
							duration: 2000,
							icon: "none",
						});
						return;
					}
					url = "user/userOpen/phoneLogin";
				}

				// if (!self.isRead) {
				// 	uni.showToast({
				// 		title: '请同意并勾选协议内容',
				// 		duration: 2000,
				// 		icon: 'none'
				// 	});
				// 	return;
				// }

				uni.showLoading({
					title: "正在提交",
				});
				console.log("start");
				self._post(
					url,
					formdata,
					(result) => {
						console.log("start1");
						console.log(result);
						// 记录token userId
						uni.setStorageSync("token", result.data.token);
						uni.setStorageSync("userId", result.data.loginUserVo.userId);
						// 获取登录前页面
						let url = "/" + uni.getStorageSync("currentPage");
						let pageOptions = uni.getStorageSync("currentPageOptions");
						if (Object.keys(pageOptions).length > 0) {
							url += "?";
							for (let i in pageOptions) {
								url += i + "=" + pageOptions[i] + "&";
							}
							url = url.substring(0, url.length - 1);
						}

						// 执行回调函数
						self.gotoPage(url);
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			},
			// resetpasswordSub() {
			// 	let self = this;
			// 	if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.resetpassword.mobile)) {
			// 		uni.showToast({
			// 			title: '手机有误,请重填！',
			// 			duration: 2000,
			// 			icon: 'none'
			// 		});
			// 		return;
			// 	}
			// 	if (self.resetpassword.code == '') {
			// 		uni.showToast({
			// 			title: '验证码不能为空！',
			// 			duration: 2000,
			// 			icon: 'none'
			// 		});
			// 		return;
			// 	}
			// 	if (self.resetpassword.password.length < 6) {
			// 		uni.showToast({
			// 			title: '密码至少6位数！',
			// 			duration: 2000,
			// 			icon: 'none'
			// 		});
			// 		return;
			// 	}
			// 	if (self.resetpassword.password !== self.resetpassword.repassword) {
			// 		uni.showToast({
			// 			title: '两次密码输入不一致！',
			// 			duration: 2000,
			// 			icon: 'none'
			// 		});
			// 		return;
			// 	}

			// 	// if (!self.isRead) {
			// 	// 	uni.showToast({
			// 	// 		title: '请同意并勾选协议内容',
			// 	// 		duration: 2000,
			// 	// 		icon: 'none'
			// 	// 	});
			// 	// 	return;
			// 	// }

			// 	// self.register.invitationId = uni.getStorageSync('invitationId') ? uni.getStorageSync('invitationId') : 0;
			// 	// self.register.regSource = 'h5';
			// 	// self.register.refereeId = uni.getStorageSync('refereeId');

			// 	uni.showLoading({
			// 		title: '正在提交'
			// 	});
			// 	self._postBody(
			// 		'user/userOpen/register',
			// 		self.register,
			// 		result => {
			// 			uni.showToast({
			// 				title: '重置成功',
			// 				duration: 3000
			// 			})
			// 			self.formData.mobile = self.resetpassword.mobile;
			// 			self.register = {
			// 				mobile: '',
			// 				password: '',
			// 				repassword: '',
			// 				code: ''
			// 			};
			// 			self.second=0;
			// 			self.changeMsg();
			// 			self.is_login = 1;
			// 		},
			// 		false,
			// 		() => {
			// 			uni.hideLoading();
			// 		}
			// 	);
			// },
			resetpasswordSub() {
				let self = this;
				if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.resetpassword.mobile)) {
					uni.showToast({
						title: "手机有误,请重填！",
						duration: 2000,
						icon: "none",
					});
					return;
				}
				if (self.resetpassword.code == "") {
					uni.showToast({
						title: "验证码不能为空！",
						duration: 2000,
						icon: "none",
					});
					return;
				}
				if (self.resetpassword.password.length < 6) {
					uni.showToast({
						title: "密码至少6位数！",
						duration: 2000,
						icon: "none",
					});
					return;
				}
				if (self.resetpassword.password !== self.resetpassword.repassword) {
					uni.showToast({
						title: "两次密码输入不一致！",
						duration: 2000,
						icon: "none",
					});
					return;
				}

				uni.showLoading({
					title: "正在提交",
				});
				self._post(
					"user/userOpen/resetpassword",
					self.resetpassword,
					(result) => {
						uni.showToast({
							title: "重置成功",
							duration: 3000,
						});
						self.formData.mobile = self.resetpassword.mobile;
						self.resetpassword = {
							mobile: "",
							password: "",
							repassword: "",
							code: "",
						};
						self.second = 0;
						self.changeMsg();
						self.is_login = 1;
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			},
			isCode() {
				if (this.is_code) {
					this.is_code = false;
				} else {
					this.is_code = true;
				}

				console.log(this.is_code);
			},
			/*发送短信*/
			sendCode() {
				let self = this;
				if (self.is_login != 0) {
					if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.formData.mobile)) {
						uni.showToast({
							title: "手机有误,请重填！",
							duration: 2000,
							icon: "none",
						});
						return;
					}
				} else if (self.is_login == 0) {
					if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.resetpassword.mobile)) {
						uni.showToast({
							title: "手机有误,请重填！",
							duration: 2000,
							icon: "none",
						});
						return;
					}
				}

				// let type = 'register'
				// let mobile = self.register.mobile
				// if (self.is_login == 1) {
				// 	type = 'login';
				// 	mobile = self.formData.mobile;
				// } else if (self.is_login == 0) {
				// 	type = 'login';
				// 	mobile = self.resetpassword.mobile;
				// }
				let type = "sms";
				let mobile = self.formData.mobile;
				if (self.is_login == 0) {
					mobile = self.resetpassword.mobile;
					let type = "login";
				}
				self._post(
					"user/userOpen/sendCode", {
						mobile: mobile,
						type: type,
					},
					(result) => {
						if (result.code == 1) {
							uni.showToast({
								title: "发送成功",
							});
							self.is_send = true;
							self.changeMsg();
						}
					}
				);
			},
			/*改变发送验证码按钮文本*/
			changeMsg() {
				if (this.second > 0) {
					this.send_btn_txt = this.second + "秒";
					this.second--;
					setTimeout(this.changeMsg, 1000);
				} else {
					this.send_btn_txt = "获取验证码";
					this.second = 60;
					this.is_send = false;
				}
			},
			// xieyi(type) {
			// 	let url = '';
			// 	if(type == 'service'){
			// 		url = this.service;
			// 	}else{
			// 		url = this.privacy;
			// 	}
			// 	uni.navigateTo({
			// 		url: '/pages/webview/webview?url=' + url
			// 	});
			// }
		},
	};
</script>

<style lang="scss" scoped>
	page {
		background: #ffffff;
	}

	@import "./login.scss";
</style>